iT邦幫忙

2025 iThome 鐵人賽

DAY 4
0
自我挑戰組

用 Discord Bot 玩轉 DevOps系列 第 4

將discord bot上線

  • 分享至 

  • xImage
  •  

今天的目標就是先讓一個最基本的 Discord Bot 成功上線,寫一個很簡易的程式使它根據我的指令給出相應的回覆


Step 1 :
首先建立一個資料夾後,在裡面新增一個檔案命名以.py結尾,接著在此資料夾打開終端模式輸入pip install discord.py安裝 discord.py套件

Step 2 :
安裝完成後可以來寫讓bot運作的程式碼
https://ithelp.ithome.com.tw/upload/images/20250917/20169329nRfBEVN6yl.png
https://ithelp.ithome.com.tw/upload/images/20250917/20169329QmGN69uFxj.png

Step 3 :
儲存後回到終端模式輸入python 檔案名.py,測試程式碼是否正常運作,成功會如下圖:
https://ithelp.ithome.com.tw/upload/images/20250917/20169329awbAI2WJLF.png
並且會看到已登入為 XXXBot編號

Step 4 :
接著回到discord發送訊息測試看看機器人有沒有回覆
https://ithelp.ithome.com.tw/upload/images/20250917/20169329wJvZiWgVYp.png

說明一下在hello前加上驚嘆號的用意是告訴bot這段文字要當作指令來處理,程式碼中

bot = commands.Bot(command_prefix="!", intents=intents)

可以看到我們設定以「!」當作前缀詞
若不這麼做區分Bot就會把每句包含 "hello" 的訊息都當成指令,比如有人聊天說「大家好 hello」,Bot 也會跳出來回覆


上一篇
淺談Github Actions
系列文
用 Discord Bot 玩轉 DevOps4
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言